﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
using ExcelLibrary.SpreadSheet;
namespace BBMS.DLL.Forms.Bill.Import
{
    public partial class frmImportFilter : FrmFilterBase
    {
        public frmImportFilter()
        {
            InitializeComponent();
            GetMaNCCList();
            GetQuayList();
        }

        private void GetMaNCCList()
        {
            ArrayList arret = NhaCungCapList.getAllMaNCC();
            cbxMaNCC.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxMaNCC.Items.Add(kvp);
                }
            }
            cbxMaNCC.SelectedIndex = 0;
        }

        private void GetQuayList()
        {
            ArrayList arret = QuayList.getAllMaQuay();
            cbxQuay.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxQuay.Items.Add(kvp);
                }
            }
            cbxQuay.SelectedIndex = 0;
        }

        public new static readonly string FunctionKey = "CT_HDN";
        protected override string GetFunctionKey() { return "CT_HDN"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new HoaDonNhap(); }

        protected override void GenColHeaders()
        {
            lvList.Columns.Clear();
            lvList.Columns.Add("Ký hiệu", 80);
            lvList.Columns[0].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Số hiệu", 80);
            lvList.Columns[1].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Ngày HĐ", 80);
            lvList.Columns[2].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Nhà cung cấp", 200);
            lvList.Columns.Add("Mã quầy", 80);
            lvList.Columns[4].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Quầy", 150);
            lvList.Columns.Add("Giá trị chưa thuế", 100);
            lvList.Columns[6].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Thuế suất", 80);
            lvList.Columns[7].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Giá trị thuế", 100);
            lvList.Columns[8].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Giá trị hóa đơn", 100);
            lvList.Columns[9].TextAlign = HorizontalAlignment.Right;
        }
        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Mã chứng từ", "Số chứng từ", "Ngày chứng từ", "Hạn thanh toán",
            "Ngày thanh toán dự kiến" , "Ngày thanh toán", "Trạng thái thanh toán", "Mã hồ sơ",
            "Mã NCC", "Mã nhà cung cấp", "Giá trị hàng hóa","Chiết khấu", "Giá trị chưa thuế", "Thuế suất", "Giá trị thuế",
            "Giá trị chứng từ" , "Giá trị đã thanh toán", "Ghi chú", "Mã nhân viên"});
            HoaDonNhap2 hoaDonNhap2List = new HoaDonNhap2();
            ArrayList arr = hoaDonNhap2List.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    HoaDonNhap2 hdn = (HoaDonNhap2)arr[i];
                    ar.Add(new string[] { hdn.ID.ToString(), hdn.MaCT, hdn.SoCT, hdn.NgayCT.ToString(), hdn.NgayHT.ToString()
                    , hdn.NgayTTDuKien.ToString(),hdn.NgayTT.ToString(), ((int)hdn.TrangThaiTT).ToString()
                    , hdn.MaHoSo, hdn.MaNCC, hdn.MaQuay, hdn.GiaTriHangHoa.ToString(),
                    hdn.ChietKhau.ToString(),hdn.GiaTriChuaThue.ToString(),hdn.ThueSuat.ToString(),
                    hdn.GiaTriThue.ToString(),hdn.GiaTriCT.ToString(),hdn.GiaTriDaTT.ToString(),
                    hdn.GhiChu.ToString(),hdn.NVID.ToString()});
                }
            }
            //ArrayList ar = new ArrayList();
            ////ar.Add(new string[] { "Mã NCC", "Tên NCC", "Địa chỉ", "Thành phố", });
            ////ar.Add(new string[] { "QTP", "Quầy thực phẩm", "Food counter", "NTP" });
            //// define col width
            ////ar.Add(new int[] { 100, 300, 100, 300 });
            return ar;
        }


        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                HoaDonNhap2 hdn = new HoaDonNhap2();     
                Cell cell = sheet.Cells[i, 0];
                if (cell.Value.ToString() != "") hdn.ID = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 1];
                hdn.MaCT = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                hdn.SoCT = cell.Value.ToString();
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") hdn.NgayCT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];
                if (cell.Value.ToString() != "") hdn.NgayHT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 5];
                if (cell.Value.ToString() != "") hdn.NgayTTDuKien = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 6];
                if (cell.Value.ToString() != "") hdn.NgayTT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 7];
                foreach (TrangThaiThanhToan item in Enum.GetValues(typeof(TrangThaiThanhToan)))
                {
                    if (item.ToString() == cell.Value.ToString())
                        hdn.TrangThaiTT = item;
                }
                
                cell = sheet.Cells[i, 8];
                hdn.MaHoSo = cell.Value.ToString();
                cell = sheet.Cells[i, 9];
                hdn.MaNCC = cell.Value.ToString();
                cell = sheet.Cells[i, 10];
                hdn.MaQuay = cell.Value.ToString();
                cell = sheet.Cells[i, 11];
                if (cell.Value.ToString() != "") hdn.GiaTriHangHoa = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 12];
                if (cell.Value.ToString() != "") hdn.ChietKhau = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 13];
                if (cell.Value.ToString() != "") hdn.GiaTriChuaThue = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 14];
                if (cell.Value.ToString() != "") hdn.ThueSuat = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 15];
                if (cell.Value.ToString() != "") hdn.GiaTriThue = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 16];
                if (cell.Value.ToString() != "") hdn.GiaTriCT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 17];
                if (cell.Value.ToString() != "") hdn.GiaTriDaTT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 18];
                hdn.GhiChu = cell.Value.ToString();
                cell = sheet.Cells[i, 19];
                if (cell.Value.ToString() != "") hdn.NVID = Convert.ToInt32(cell.Value.ToString());
                arret.Add(hdn);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {

            if (txtMa.Text.Trim() != "" || txtSoCT.Text.Trim() != "" || cbxMaNCC.SelectedIndex != -1 || cbxQuay.SelectedIndex != -1
            || txtGiaTriCTTu.Text.Trim() != "0" || txtGiaTriCTDen.Text.Trim() != "0")
            {
                fParams = new FilterParams();
                //FilterCondition fcLoaiCTCK = new FilterCondition("LoaiCT", "LoaiCT", "HDX", SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcLoaiCTCK);
                //FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcIsChi);
                if (txtMa.Text.Trim() != "")
                {
                    FilterCondition fcMa = new FilterCondition("MaCT", "MaCT", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcMa);
                }

                if (txtSoCT.Text.Trim() != "")
                {
                    FilterCondition fcSoCT = new FilterCondition("SoCT", "SoCT", "%" + txtSoCT.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcSoCT);
                }

                if (cbxMaNCC.SelectedIndex != 0)
                {
                    FilterCondition fcMaNCC = new FilterCondition("MaNCC", "MaNCC", ((KeyValuePair)cbxMaNCC.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaNCC);
                }

                if (cbxQuay.SelectedIndex != 0)
                {
                    FilterCondition fcMaQuay = new FilterCondition("MaQuay", "MaQuay", ((KeyValuePair)cbxQuay.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaQuay);
                }

                if (txtGiaTriCTTu.Text.Trim() != "" && txtGiaTriCTTu.Text.Trim() != "0")
                {
                    FilterCondition fcGiaTriCTTu = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTTu.Text.Trim().Replace(",", ""), SQLOperator.GREATER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTTu);
                }

                if (txtGiaTriCTDen.Text.Trim() != "" && txtGiaTriCTDen.Text.Trim() != "0")
                {
                    FilterCondition fcGiaTriCTDen = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTDen.Text.Trim().Replace(",", ""), SQLOperator.LOWER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTDen);
                }


                fParams.CurrentPage = 0;
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }
        }



        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmImportUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            MethodInvoker updateListAsync = delegate
            {
                if (obj == null) return;
                ListViewItem it = new ListViewItem(((HoaDonNhap)obj).MaCT);
                it.SubItems.Add(((HoaDonNhap)obj).SoCT);
                it.SubItems.Add(((HoaDonNhap)obj).NgayCT.ToString("dd/MM/yyyy"));
                it.SubItems.Add(ObjectLists.NhaCungCapList.getObjectName(((HoaDonNhap)obj).MaNCC));
                it.SubItems.Add(((HoaDonNhap)obj).MaQuay);
                it.SubItems.Add(ObjectLists.QuayList.getObjectName(((HoaDonNhap)obj).MaQuay));
                it.SubItems.Add(CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriCT));
                it.SubItems.Add(CommonTools.Helper.PercentString(((HoaDonNhap)obj).ThueSuat));
                it.SubItems.Add(CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriThue));
                it.SubItems.Add(CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriCT));
                it.Tag = obj;
                lvList.Items.Add(it);

            };
            if (lvList.InvokeRequired)
                lvList.Invoke(updateListAsync);
            else
                updateListAsync();

        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((HoaDonNhap)it.Tag).MaCT == ((HoaDonNhap)obj).MaCT)
                {
                    it.SubItems[2].Text = ((HoaDonNhap)obj).NgayCT.ToString("dd/MM/yyyy");
                    it.SubItems[3].Text = ObjectLists.NhaCungCapList.getObjectName(((HoaDonNhap)obj).MaNCC);
                    it.SubItems[4].Text = ((HoaDonNhap)obj).MaQuay;
                    it.SubItems[5].Text = ObjectLists.QuayList.getObjectName(((HoaDonNhap)obj).MaQuay);
                    it.SubItems[6].Text = CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriCT);
                    it.SubItems[7].Text = CommonTools.Helper.PercentString(((HoaDonNhap)obj).ThueSuat);
                    it.SubItems[8].Text = CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriThue);
                    it.SubItems[9].Text = CommonTools.Helper.MoneyString(((HoaDonNhap)obj).GiaTriCT);
                    it.Tag = obj;
                    break;
                }
            }
        }
    }
}
